package io;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: ycy
 * @CreateDate: 2019/4/2 15:24
 */
public class NoneResIODemo {

  public static void main(String[] args) {
    NoneResIODemo noneResIODemo = new NoneResIODemo();
    List<String> htttpUrls = noneResIODemo.getHtttpUrl();
    List<Long> costTime = Lists.newLinkedList();
    int invalid = 0;
    int cnt = 0;
    System.out.println(htttpUrls.size());
    System.out.println(invalid);
    System.out.println(cnt);

    int totalCostTime = 0;
    for (Long aLong : costTime) {
      totalCostTime += aLong;
    }
    System.out.println("总耗时" + totalCostTime + ",  " + costTime.size());

    /* 结果
    11514    -->0.859%
    99
    11514
    总耗时 564693,  11415    -->49.4 ms
    */
    Set<String> dup = Sets.newHashSet(htttpUrls);
    noneResIODemo.printOutFile(dup);
  }

  public void printOutFile(Set<String> idNameList) {

    try {
      PrintWriter pw = new PrintWriter(new PrintStream(new FileOutputStream("\\Users\\yincy\\Desktop\\1\\print.txt")));
      for (String alias : idNameList) {
        pw.println(alias);
      }
      pw.flush();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } finally {

    }
  }

  private Pattern pattern = Pattern.compile("\\d+");

  public List<String> getHtttpUrl() {
    List<String> Urls = Lists.newLinkedList();
    File file = new File("D:\\Users\\yincy\\Desktop\\1\\engine.access.log");
    try {
      FileInputStream fis = new FileInputStream(file);
      InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
      BufferedReader br = new BufferedReader(isr);
      //一行一读
      while (br.ready()) {
        String line = br.readLine();
        if (StringUtils.isNotBlank(line)) {
          String[] split = line.split("\t");
          String url = split[9].trim();
          if (url.startsWith("/globalsearch/search")) {
            String[] urlArr = line.split(";");
            String s = urlArr[urlArr.length - 1];
//            System.out.println(s);
            Matcher matcher2 = pattern.matcher(s);
            String group = null;
            while (matcher2.find()) {
              group = matcher2.group();
            }
            try {
           /*   if (Integer.valueOf(group) > 0) {
                //获取 500ms 以上的数据
                Urls.add(group + "\t" + url);
              }*/

            /*  String[] split1 = url.split("&");
              for (String s1 : split1) {
                String[] split2 = s1.split("=");
                String s2 = split2[0];
                if ("userid".equalsIgnoreCase(s2)) {
                  if (split2.length >= 2) {
                    Urls.add(split2[1]);
                  }
                }
              }*/
              Urls.add(url);
            } catch (NumberFormatException e) {
              e.printStackTrace();
            }

          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return Urls;
  }


}
